package com.tjetc.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.tjetc.common.JsonResult;
import com.tjetc.entity.Admin;
import com.tjetc.entity.User;
import com.tjetc.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("admin/adminCounter")
public class AdminCounterController {
    @Autowired
    private AdminService adminService;

    // 计算管理员总数
    @RequestMapping("countAdmin")
    public JsonResult count(){
        return new JsonResult(0, "", adminService.countAdmin());
    }

    // 查询用户待审核的数量
    @RequestMapping("countUsers")
    public JsonResult countUserWaiting(){
        return new JsonResult<>(0, "", adminService.countUserWaiting());
    }

    /**
     * 计算需要审核的菜品数量
     * @return
     */
    @RequestMapping("countDishWaiting")
    public JsonResult countDishWaiting(){
        return new JsonResult<>(0, "", adminService.countDishWaiting());
    }

    // 列出今日订单
    @RequestMapping("listTodayOrders")
    public JsonResult getTodayOrders(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time) {
        return new JsonResult(0, "", adminService.getTodayOrders(time));
    }

    // 获取完成订单的数量
    @RequestMapping("listTodayFinishOrders")
    public JsonResult getTodayFinishOrders(
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time) {
        return new JsonResult(0, "", adminService.getTodayFinishOrders(time));
    }

    // 计算今日所有已完成订单的amount
    @RequestMapping("amountTodayOrders")
    public JsonResult getTodayOrderMoney(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time) {
        return new JsonResult(0, "", adminService.getTodayOrderMoney(time));
    }

    // 计算指定商家的今日的amount
    @RequestMapping("amountMerchantMoney")
    public JsonResult getMerchantMoney(@RequestParam("merchantId") Integer id ,
                                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time) {
        return new JsonResult<>(0, "", adminService.getTodayMerchantMoney(id, time));
    }

    // 获得指定id的Admin用户 返回前端处理对象
    @RequestMapping("getAdminFromId")
    public JsonResult getAdminInfo(Integer id) {
        return new JsonResult<>(0, "", adminService.getAdminById(id));
    }

    // 更新admin用户
    @RequestMapping("updateAdmin")
    public JsonResult updateAdminInfo(Admin admin) {
        return new JsonResult<>(0, "", adminService.update(admin));
    }

    // 传入时间 输出merchant列表
    @RequestMapping("getMerchantOrderByMoney")
    public JsonResult getMerchantOrderByTodayMoney(@RequestParam("time")
                                                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time) {
        return new JsonResult<>(0, "", adminService.getMerchantOrderByTodayMoney(time));
    }

    // 获得merhcant的money变化表格的横纵坐标
    @RequestMapping("getMerchantDaysMoney")
    public JsonResult getMerchantDaysMoney(@RequestParam("merchantId") Integer id,
                                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time,
                                           @RequestParam("day") Integer day) {
        return new JsonResult<>(0, "", adminService.getMerchantDaysMoney(id, time, day));
    }

    // 添加admin用户
    @RequestMapping("addAdmin")
    public JsonResult addAdmin(Admin admin) {
        return adminService.insert(admin);
    }

    // 查询不是管理员的用户（已弃用）
    @RequestMapping("searchUsersNotAdmin")
    public JsonResult searchUsersNotAdmin() {
        return new JsonResult<>(0, "", adminService.searchUsersNotAdmin());
    }
}
